/*
 * WartepositionMapper.java
 * 
 *  author: Daniel Rudigier
 *    date: 30.05.2008
 * 
 * 
 */
package ocumed.persistenz.mapper;

import java.util.List;

import org.hibernate.exception.ConstraintViolationException;

import ocumed.applikation.domainhandling.BenutzerErzeugung;
import ocumed.applikation.domainhandling.DomainHandler;
import ocumed.persistenz.dao.BenutzerDAO;
import ocumed.persistenz.dao.DAOFactory;
import ocumed.persistenz.dao.PatientDAO;
import ocumed.persistenz.dao.TerminDAO;
import ocumed.persistenz.dao.WartepositionDAO;
import ocumed.persistenz.hibernate.OcBenutzer;
import ocumed.persistenz.hibernate.OcPatient;
import ocumed.persistenz.hibernate.OcTermin;
import ocumed.persistenz.hibernate.OcWarteposition;
import ocumed.teams.IWarteposition;
import ocumed.teams.exceptions.PatientAlreadyEnqueuedException;

/**
 * @author Daniel Rudigier
 *
 */
public class WartepositionMapper extends PersistenceMapper {
    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#get(int)
     */
    @Override
    public Object get(int id) {
//        WartepositionDAO wDAO = DAOFactory.get(DAOFactory.ORACLE).getWartepositionDAO();
//        return wDAO.findById(id);
        throw new UnsupportedOperationException();
    }

    /**
     * override
     * @throws PatientAlreadyEnqueuedException 
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#put(int, java.lang.Class)
     */
    @Override
    public void put(Object instance) 
            throws PatientAlreadyEnqueuedException {
        WartepositionDAO wDAO = DAOFactory.get(DAOFactory.RDBMS).getWartepositionDAO();
        wDAO.start();
        
        IWarteposition i = (IWarteposition) instance; 
        OcWarteposition w = wDAO.findById(i.getWartepositionid());
        
        if (w != null) {
            updateWarteposition(w, i);
            wDAO.attachDirty(w);
        } else {
            try {
                w = new OcWarteposition();
                updateWarteposition(w, i);
                Integer id = wDAO.persist(w);
                w.setWartepositionid(id);
            } catch (ConstraintViolationException cve) {
                // already in queue
                throw new PatientAlreadyEnqueuedException();
            }
        }
        
        wDAO.commit();
    }

    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#getAll()
     */
    @Override
    public List getAll() {
//        WartepositionDAO wDAO = DAOFactory.get(DAOFactory.ORACLE).getWartepositionDAO(); 
//        wDAO.start();        
//        return wDAO.findAll();
        throw new UnsupportedOperationException();
    }
    
    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#delete(java.lang.Object)
     */
    @Override
    public void delete(Object instance) {
        WartepositionDAO wDAO = DAOFactory.get(DAOFactory.RDBMS).getWartepositionDAO(); 
        wDAO.start();
        
        IWarteposition i = (IWarteposition) instance; 
        OcWarteposition w = wDAO.findById(i.getWartepositionid());
        
        wDAO.delete(w);
        wDAO.commit();
    }
    
    /**
     * update a specific warteposition
     * 
     * @param wp
     */
    private void updateWarteposition(OcWarteposition w, IWarteposition i) {
        BenutzerDAO bDAO = DAOFactory.get(DAOFactory.RDBMS).getBenutzerDAO();
        PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
        TerminDAO tDAO = DAOFactory.get(DAOFactory.RDBMS).getTerminDAO();
        
        OcBenutzer b = i.getArzt() != null ? bDAO.findById(i.getArzt().getBenutzerid()) : null; 
        OcPatient p = i.getPatient() != null ? pDAO.findById(i.getPatient().getPatientid()) : null;
        OcTermin t = i.getTermin() != null ? tDAO.findById(i.getTermin().getTerminid()) : null;
        
        w.setOcBenutzer(b);
        w.setOcPatient(p);
        w.setOcTermin(t);
        w.setWartepositionnr(i.getWartepositionnr());
    }

}
